package cn.iocoder.yudao.module.bpm.service.projectinfo;

import cn.iocoder.yudao.module.bpm.enums.projectinfo.BpmProjectInfoStatusEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.bpm.controller.admin.projectinfo.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.projectinfo.BpmProjectInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.bpm.dal.mysql.projectinfo.BpmProjectInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;

/**
 * 流程项目信息 Service 实现类
 *
 * @author 韩佳乐
 */
@Service
@Validated
public class BpmProjectInfoServiceImpl implements BpmProjectInfoService {

    @Resource
    private BpmProjectInfoMapper projectInfoMapper;

    @Override
    public Long createProjectInfo(BpmProjectInfoSaveReqVO createReqVO) {
        // 插入
        BpmProjectInfoDO projectInfo = BeanUtils.toBean(createReqVO, BpmProjectInfoDO.class);
        projectInfo.setCreateTime(LocalDateTime.now());
        projectInfo.setUpdateTime(LocalDateTime.now());
        Long userId = getLoginUserId();
        projectInfo.setCreator(String.valueOf(userId));
        projectInfo.setUpdater(String.valueOf(userId));
        projectInfo.setStatus(BpmProjectInfoStatusEnum.NOT_STARTED.getStatus());
        projectInfoMapper.insert(projectInfo);
        // 返回
        return projectInfo.getId();
    }

    @Override
    public void updateProjectInfo(BpmProjectInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateProjectInfoExists(updateReqVO.getId());
        updateReqVO.setUpdater(String.valueOf(getLoginUserId()));
        updateReqVO.setUpdateTime(LocalDateTime.now());
        // 更新
        BpmProjectInfoDO updateObj = BeanUtils.toBean(updateReqVO, BpmProjectInfoDO.class);
        projectInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteProjectInfo(Long id) {
        // 校验存在
        validateProjectInfoExists(id);
        // 删除
        projectInfoMapper.deleteById(id);
    }

    private void validateProjectInfoExists(Long id) {
        if (projectInfoMapper.selectById(id) == null) {
            throw exception(PROJECT_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BpmProjectInfoDO getProjectInfo(Long id) {
        return projectInfoMapper.selectById(id);
    }

    @Override
    public PageResult<BpmProjectInfoDO> getProjectInfoPage(BpmProjectInfoPageReqVO pageReqVO) {
        return projectInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BpmProjectInfoDO> getProjectInfos() {
        return projectInfoMapper.selectList();
    }
}